package com.fowo.api.syncAmazon.api;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.amazon.spapi.api.FinancesApi;
import com.amazon.spapi.api.ReportsApi;
import com.amazon.spapi.client.ApiException;
import com.amazon.spapi.model.finances.*;
import com.amazon.spapi.model.finances.Currency;
import com.amazon.spapi.model.reports.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.datainterface.entity.DatainterfaceAuthority;
import com.fowo.api.datainterface.service.DatainterfaceAuthorityService;
import com.fowo.api.entity.*;
import com.fowo.api.service.impl.*;
import com.fowo.api.syncAmazon.util.AmazonUtil;
import com.fowo.api.syncAmazon.util.AmzDateUtils;
import com.fowo.api.syncAmazon.util.GeneralUtil;
import com.fowo.api.sys.model.NewSerialRequest;
import com.fowo.api.sys.service.SysSerialService;
import com.squareup.okhttp.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.threeten.bp.OffsetDateTime;
import org.threeten.bp.ZoneOffset;

import javax.annotation.Resource;
import java.io.*;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

@Component
@Slf4j
public class SettlementReportService extends ReportService {
    @Resource
    private DatainterfaceAuthorityService datainterfaceAuthorityBaseService;
    @Resource
    private AmazonUtil amazonUtil;
    @Resource
    private ShopInfoServiceImpl shopInfoService;
    @Resource
    private ShopAuthorizationServiceImpl shopAuthorizationService;
    @Resource
    private TmpSettlementReportServiceImpl tmpSettlementReportService;
    @Resource
    private TmpFinancialEventGroupServiceImpl tmpFinancialEventGroupService;
    @Resource
    protected SysSerialService sysSerialService;
    @Resource
    private SysRegionServiceImpl sysRegionService;

    private String financialNo;
    private List<SysRegion> regionList;

    @Override
    public void download(String url, String compressionAlgorithm, ShopInfo shop) throws IOException, IllegalArgumentException {

    }

    /**
     * 结算中心明细
     * @throws Exception
     */
    public void getReports() throws Exception {
        DatainterfaceAuthority entityAuthority = datainterfaceAuthorityBaseService.getOne(Wrappers.<DatainterfaceAuthority>lambdaQuery().eq(DatainterfaceAuthority::getAppCode,"Amazon"), false);

        final List<ShopAuthorization> authorizationList = shopAuthorizationService.list(Wrappers.lambdaQuery(ShopAuthorization.class)
                .eq(ShopAuthorization::getStatus, "SQ01"));

        final List<ShopInfo> list = shopInfoService.list(Wrappers.lambdaQuery(ShopInfo.class)
                .eq(ShopInfo::getPlatform,"Amazon")
                .eq(ShopInfo::getSellerAuthStatus,"SQ01"));
        for(ShopAuthorization authorization : authorizationList){
            List<ShopInfo> shopList = list.stream().filter(s -> s.getParentShopAuthorization().equals(authorization.getId())).collect(Collectors.toList());
            ShopInfo shop = shopList.get(0);
            ReportsApi reportsApi = amazonUtil.getReportsApi(entityAuthority, shop,0.0222);
            List<String> reportTypes = new ArrayList<>();
            reportTypes.add("GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2");
            List<String> processingStatuses = new ArrayList<>();
            processingStatuses.add("DONE");
            List<String> marketplaceIds = shopList.stream().map(s -> s.getMarketplaceId()).collect(Collectors.toList());
//            marketplaceIds.add(shop.getMarketplaceId());
            Integer pageSize = 100;
            Calendar cstart=Calendar.getInstance();
            cstart.add(Calendar.DATE, -60);
            OffsetDateTime createdSince = AmzDateUtils.getOffsetDateTimeUTC(cstart);
            OffsetDateTime createdUntil = null;
            String nextToken = null;
            final GetReportsResponse reports = reportsApi.getReports(reportTypes, processingStatuses, marketplaceIds, pageSize, createdSince, createdUntil, nextToken);
            System.out.println(reports.toString());
            if(null == reports){
                continue;
            }
            nextToken = reports.getNextToken();
            if(StrUtil.isNotBlank(nextToken)){

            }
            for(Report report : reports.getReports()){
                // 第三步 获取报告连接
                ReportsApi reportsApi3 = amazonUtil.getReportsApi(entityAuthority,shop,0.0167);
                final ReportDocument reportDocument = reportsApi3.getReportDocument(report.getReportDocumentId());
                // 第四步 下载报告
                String compressionAlgorithm = null;
                if(null != reportDocument.getCompressionAlgorithm()){
                    compressionAlgorithm = reportDocument.getCompressionAlgorithm().getValue();
                }
                download(reportDocument.getUrl(),compressionAlgorithm,shopList);
            }
        }
    }
    public void getNextReport(ReportsApi reportsApi,String nextToken) throws ApiException {
        final GetReportsResponse reports = reportsApi.getReports(null, null, null, null, null, null, nextToken);
    }
    public void download(String url, String compressionAlgorithm, List<ShopInfo> shopList) throws IOException, IllegalArgumentException {
        OkHttpClient httpclient = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .get()
                .build();

        Response response = httpclient.newCall(request).execute();
        if (!response.isSuccessful()) {
            System.out.println(
                    String.format("Call to download content was unsuccessful with response code: %d and message: %s",
                            response.code(), response.message()));
            return;
        }

        try (ResponseBody responseBody = response.body()) {
            MediaType mediaType = MediaType.parse(response.header("Content-Type"));
//            Charset charset = mediaType.charset();
//            if (charset == null) {
//                throw new IllegalArgumentException(String.format(
//                        "Could not parse character set from '%s'", mediaType.toString()));
//            }

            Closeable closeThis = null;
            try {
                InputStream inputStream = responseBody.byteStream();
                closeThis = inputStream;

                if ("GZIP".equals(compressionAlgorithm)) {
                    inputStream = new GZIPInputStream(inputStream);
                    closeThis = inputStream;
                }

                // This example assumes that the download content has a charset in the content-type header, e.g.
                // text/plain; charset=UTF-8
                if ("text".equals(mediaType.type()) && "plain".equals(mediaType.subtype())) {
                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
                    closeThis = inputStreamReader;

                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    closeThis = reader;

                    int lineNumber = 0;
                    String line;
                    Map<String,Integer> titleList = new HashMap<String,Integer>();
                    List<TmpSettlementReport> list = new ArrayList<>();
                    String currency = null;
                    String settlement_start_date = null;
                    String settlement_end_date = null;
                    String deposit_date = null;
                    while ((line = reader.readLine()) != null) {
                        String[] info = line.split("\t");
                        int length = info.length;
                        if (lineNumber == 0) {
                            for (int i = 0; i < length; i++) {
                                titleList.put(info[i],i);
                            }
                        } else {
                            TmpSettlementReport tmp = new TmpSettlementReport();
                            tmp.setCreateTime(new Date());
                            tmp.setLastUpdateTime(new Date());

                            String settlement_id= GeneralUtil.getStrValue(info, titleList, "settlement-id");
                            if(StrUtil.isBlank(settlement_start_date)){
                                settlement_start_date= GeneralUtil.getStrValue(info, titleList, "settlement-start-date");
                            }
                            if(StrUtil.isBlank(settlement_end_date)){
                                settlement_end_date= GeneralUtil.getStrValue(info, titleList, "settlement-end-date");
                            }
                            if(StrUtil.isBlank(settlement_end_date)){
                                deposit_date= GeneralUtil.getStrValue(info, titleList, "deposit-date");
                            }
                            if(StrUtil.isBlank(currency)){
                                currency= GeneralUtil.getStrValue(info, titleList, "currency");
                            }

                            String order_id = GeneralUtil.getStrValue(info, titleList, "order-id");
                            if(StrUtil.isBlank(order_id)){
                                continue;
                            }
//                            BigDecimal total_amount= GeneralUtil.getBigDecimalValue(info, titleList, "total-amount");
                            String transaction_type= GeneralUtil.getStrValue(info, titleList, "transaction-type");
                            String merchant_order_id = GeneralUtil.getStrValue(info, titleList, "merchant-order-id");
                            String adjustment_id= GeneralUtil.getStrValue(info, titleList, "adjustment-id");
                            String shipment_id= GeneralUtil.getStrValue(info, titleList, "shipment-id");
                            String marketplace_name= GeneralUtil.getStrValue(info, titleList, "marketplace-name");
                            String amount_type= GeneralUtil.getStrValue(info, titleList, "amount-type");
                            String amount_description= GeneralUtil.getStrValue(info, titleList, "amount-description");
                            BigDecimal amount= GeneralUtil.getBigDecimalValue(info, titleList, "amount");
                            String fulfillment_id= GeneralUtil.getStrValue(info, titleList, "fulfillment-id");
                            String posted_date= GeneralUtil.getStrValue(info, titleList, "posted-date");
                            String posted_date_time= GeneralUtil.getStrValue(info, titleList, "posted-date-time");
                            String order_item_code= GeneralUtil.getStrValue(info, titleList, "order-item-code");
                            String merchant_order_item_id= GeneralUtil.getStrValue(info, titleList, "merchant-order-item-id");
                            String merchant_adjustment_item_id= GeneralUtil.getStrValue(info, titleList, "merchant-adjustment-item-id");
                            String sku= GeneralUtil.getStrValue(info, titleList, "sku");
                            Integer quantity_purchased= GeneralUtil.getIntegerValue(info, titleList, "quantity-purchased");

                            String region = currency.substring(0,2);
                            ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                            tmp.setSid(shopInfo.getId());
                            tmp.setSettlementId(settlement_id);
                            tmp.setSettlementStartDate(StrUtil.isBlank(settlement_start_date) ? null : GeneralUtil.getDatez(settlement_start_date.substring(0,19)));
                            tmp.setSettlementEndDate(StrUtil.isBlank(settlement_end_date) ? null : GeneralUtil.getDatez(settlement_end_date.substring(0,19)));
                            tmp.setDepositDate(StrUtil.isBlank(deposit_date) ? null : GeneralUtil.getDatez(deposit_date.substring(0,19)));
                            tmp.setCurrency(currency);
                            tmp.setTransactionType(transaction_type);
                            tmp.setOrderId(order_id);
                            tmp.setMerchantOrderId(merchant_order_id);
                            tmp.setAdjustmentId(adjustment_id);
                            tmp.setShipmentId(shipment_id);
                            tmp.setMarketplaceName(marketplace_name);
                            tmp.setAmountType(amount_type);
                            tmp.setAmountDescription(amount_description);
                            tmp.setAmount(amount);
                            tmp.setFulfillmentId(fulfillment_id);
                            tmp.setPostedDate(StrUtil.isBlank(posted_date) ? null : GeneralUtil.getDatez(posted_date));
                            tmp.setPostedDateTime(StrUtil.isBlank(posted_date_time) ? null : GeneralUtil.getDatez(posted_date_time.substring(0,19)));
                            tmp.setOrderItemCode(order_item_code);
                            tmp.setMerchantOrderItemId(merchant_order_item_id);
                            tmp.setMerchantAdjustmentItemId(merchant_adjustment_item_id);
                            tmp.setMsku(sku);
                            tmp.setQuantityPurchased(quantity_purchased);
                            list.add(tmp);
                        }
                        lineNumber++;
                        // Process line by line.
                    }
                    tmpSettlementReportService.saveBatch(list);
                } else {
                    //Handle content with binary data/other media types here.
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                log.error("处理结算中心明细数据异常",e);
            } finally {
                if (closeThis != null) {
                    closeThis.close();
                }
            }
        }
    }

    public void getListFinancialEvents() throws ApiException {
        regionList = sysRegionService.list();
        DatainterfaceAuthority entityAuthority = datainterfaceAuthorityBaseService.getOne(Wrappers.<DatainterfaceAuthority>lambdaQuery().eq(DatainterfaceAuthority::getAppCode,"Amazon"), false);

        final List<ShopAuthorization> authorizationList = shopAuthorizationService.list(Wrappers.lambdaQuery(ShopAuthorization.class)
                .eq(ShopAuthorization::getStatus, "SQ01"));

        final List<ShopInfo> list = shopInfoService.list(Wrappers.lambdaQuery(ShopInfo.class)
                .eq(ShopInfo::getPlatform,"Amazon")
                .eq(ShopInfo::getSellerAuthStatus,"SQ01"));

        List<TmpFinancialEventGroup> tmpGroupList = new ArrayList<>();
        for(ShopAuthorization shopAuthorization : authorizationList){
            List<ShopInfo> shopList = list.stream().filter(s -> s.getParentShopAuthorization().equals(shopAuthorization.getId())).collect(Collectors.toList());
            ShopInfo shopInfo = shopList.get(0);
            FinancesApi financesApi = amazonUtil.getFinancesApi(entityAuthority, shopInfo);
            Integer maxResultsPerPage = 50;
            Calendar cstart=Calendar.getInstance();
            cstart.add(Calendar.HOUR_OF_DAY,-2);
            OffsetDateTime postedBefore = AmzDateUtils.getOffsetDateTimeUTC(cstart);

            cstart.add(Calendar.DATE, -60);
            OffsetDateTime postedAfter = AmzDateUtils.getOffsetDateTimeUTC(cstart);

            String nextToken = null;
            ListFinancialEventGroupsResponse response = financesApi.listFinancialEventGroups(maxResultsPerPage, postedBefore, postedAfter, nextToken);
//            System.out.println(response);
            final ListFinancialEventGroupsPayload payload = response.getPayload();
            final FinancialEventGroupList financialEventGroupList = payload.getFinancialEventGroupList();
            for(FinancialEventGroup financialEventGroup : financialEventGroupList){
//                System.out.println(financialEventGroup);
                financialNo = sysSerialService.getNewSerial(new NewSerialRequest(null, "AmazonFinancialEventGroup.financialNo"));
                //构建汇总数据
                buildFinancialEventGroup(shopList,financialEventGroup,tmpGroupList);
                //获取详情数据
                getFinancialEventsByGroupId(shopList,financesApi,financialEventGroup);
            }

            nextToken = payload.getNextToken();
            if(StrUtil.isNotBlank(nextToken)){
                getNext(financesApi,shopList,tmpGroupList, nextToken);
            }
        }
        tmpFinancialEventGroupService.saveBatch(tmpGroupList);

    }

    /**
     * 事件组下一页
     * @param financesApi
     * @param shopList
     * @param nextToken
     * @throws ApiException
     */
    public void getNext(FinancesApi financesApi ,List<ShopInfo> shopList,List<TmpFinancialEventGroup> tmpGroupList, String nextToken) throws ApiException {
        ListFinancialEventGroupsResponse response = financesApi.listFinancialEventGroups(null, null, null, nextToken);
        final ListFinancialEventGroupsPayload payload = response.getPayload();
        final FinancialEventGroupList financialEventGroupList = payload.getFinancialEventGroupList();
        for(FinancialEventGroup financialEventGroup : financialEventGroupList){
            financialNo = sysSerialService.getNewSerial(new NewSerialRequest(null, "AmazonFinancialEventGroup.financialNo"));
            //构建汇总数据
            buildFinancialEventGroup(shopList,financialEventGroup,tmpGroupList);
            //获取详情数据
            getFinancialEventsByGroupId(shopList,financesApi,financialEventGroup);
        }
        final String next = payload.getNextToken();
        if(StrUtil.isNotBlank(next)){
            getNext(financesApi,shopList,tmpGroupList, next);
        }
    }

    public void buildFinancialEventGroup(List<ShopInfo> shopList,FinancialEventGroup financialEventGroup,List<TmpFinancialEventGroup> tmpList){
        TmpFinancialEventGroup tmp = new TmpFinancialEventGroup();
        tmp.setFinancialNo(financialNo);
        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
        tmp.setFundTransferStatus(financialEventGroup.getFundTransferStatus());
        tmp.setOriginalTotal(financialEventGroup.getOriginalTotal() == null ? BigDecimal.ZERO : financialEventGroup.getOriginalTotal().getCurrencyAmount());
        tmp.setConvertedTotal(financialEventGroup.getConvertedTotal() == null ? BigDecimal.ZERO : financialEventGroup.getConvertedTotal().getCurrencyAmount());
        if(StrUtil.isNotBlank(financialEventGroup.getFundTransferDate())){
            SimpleDateFormat utcFormat = new SimpleDateFormat(DatePattern.UTC_PATTERN);
            utcFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
            try {
                Date date = utcFormat.parse(financialEventGroup.getFundTransferDate());
                final Instant instant = date.toInstant();
                final ZonedDateTime zonedDateTime = instant.atZone(ZoneId.of("Asia/Shanghai"));
                tmp.setFundTransferDate(DateUtil.date(zonedDateTime));
            } catch (ParseException e) {
                e.printStackTrace();
            }


            tmp.setFundTransferDateLocale(DateUtil.parse(financialEventGroup.getFundTransferDate(),DatePattern.UTC_PATTERN));
        }
        tmp.setTraceId(financialEventGroup.getTraceId());
        tmp.setAccountTail(financialEventGroup.getAccountTail());
        tmp.setBeginningBalance(financialEventGroup.getBeginningBalance() == null ? BigDecimal.ZERO : financialEventGroup.getBeginningBalance().getCurrencyAmount());
        tmp.setFinancialEventGroupStart(StrUtil.isNotBlank(financialEventGroup.getFinancialEventGroupStart()) ? DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(),DatePattern.UTC_PATTERN) : null);
        tmp.setFinancialEventGroupEnd(StrUtil.isNotBlank(financialEventGroup.getFinancialEventGroupEnd()) ? DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(),DatePattern.UTC_PATTERN) : null);

        String currencyType = "";
        if(financialEventGroup.getOriginalTotal() != null){
            currencyType = financialEventGroup.getOriginalTotal().getCurrencyCode();
            tmp.setOriginalTotalCurrencyCode(financialEventGroup.getOriginalTotal().getCurrencyCode());
        }else if(financialEventGroup.getConvertedTotal() != null){
            currencyType = financialEventGroup.getConvertedTotal().getCurrencyCode();
            tmp.setConvertedTotalCurrencyCode(financialEventGroup.getConvertedTotal().getCurrencyCode());
        }else if(financialEventGroup.getBeginningBalance() != null){
            currencyType = financialEventGroup.getBeginningBalance().getCurrencyCode();
        }
        tmp.setCurrencyType(currencyType);

        String region = currencyType.substring(0,2);
        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
        tmp.setSid(shopInfo.getId());
        tmp.setStation(shopInfo.getCountry());
        tmp.setStoreName(shopInfo.getShopName());
        tmp.setCountryCode(regionList.stream().filter(r -> r.getCode().equals(region)).findAny().map(r -> r.getId()).orElse(null));
        tmp.setSellerId(shopInfo.getSellerId());

        tmpList.add(tmp);
    }

    /**
     * 根据财务事件组获取财务事件
     * @param shopList
     * @param financesApi
     * @param financialEventGroup
     * @throws ApiException
     */
    public void getFinancialEventsByGroupId(List<ShopInfo> shopList,FinancesApi financesApi,FinancialEventGroup financialEventGroup) throws ApiException {
        final ListFinancialEventsResponse listFinancialEventsResponse = financesApi.listFinancialEventsByGroupId(financialEventGroup.getFinancialEventGroupId(), null,null);
        final ListFinancialEventsPayload payload = listFinancialEventsResponse.getPayload();
        final FinancialEvents financialEvents = payload.getFinancialEvents();
//        System.out.println(JSONUtil.toJsonStr(financialEvents));
        final List<TmpSettlementReport> tmpList = buildSettlement(shopList, financialEvents,financialEventGroup);
        String nextToken = payload.getNextToken();
        if(StrUtil.isNotBlank(nextToken)){
            tmpList.addAll(getFinancialEventsByGroupIdNext(financesApi,shopList,financialEventGroup,nextToken));
        }
        tmpSettlementReportService.saveBatch(tmpList);
    }

    /**
     * 事件组事件下一页
     * @param financesApi
     * @param shopList
     * @param financialEventGroup
     * @param nextToken
     * @return
     * @throws ApiException
     */
    public List<TmpSettlementReport> getFinancialEventsByGroupIdNext(FinancesApi financesApi ,List<ShopInfo> shopList,FinancialEventGroup financialEventGroup, String nextToken) throws ApiException {
        final ListFinancialEventsResponse listFinancialEventsResponse = financesApi.listFinancialEventsByGroupId(financialEventGroup.getFinancialEventGroupId(), null, nextToken);
        final ListFinancialEventsPayload payload = listFinancialEventsResponse.getPayload();
        final FinancialEvents financialEvents = payload.getFinancialEvents();
        List<TmpSettlementReport> tmpList = buildSettlement(shopList, financialEvents,financialEventGroup);
        final String next = payload.getNextToken();
        if(StrUtil.isNotBlank(next)){
            tmpList.addAll(getFinancialEventsByGroupIdNext(financesApi,shopList,financialEventGroup,next));
        }
        return tmpList;
    }

    /**
     * 构建财务信息数据
     * @param shopList
     * @param financialEvents
     */
    public List<TmpSettlementReport> buildSettlement(List<ShopInfo> shopList,FinancialEvents financialEvents,FinancialEventGroup financialEventGroup){
        List<TmpSettlementReport> list = new ArrayList<>();
        //Adjustment 卖方帐户的调整事件信息列表。
        final AdjustmentEventList adjustmentEventList = financialEvents.getAdjustmentEventList();
        if(CollectionUtil.isNotEmpty(adjustmentEventList)){
            for(AdjustmentEvent adjustmentEvent : adjustmentEventList){
                final String postedDate = adjustmentEvent.getPostedDate();
                final Currency adjustmentAmount = adjustmentEvent.getAdjustmentAmount();
                if(null != adjustmentAmount && adjustmentAmount.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("Adjustment");
                    tmp.setAmountDescription(adjustmentEvent.getAdjustmentType());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    tmp.setCurrency(adjustmentAmount.getCurrencyCode());
                    tmp.setAmount(adjustmentAmount.getCurrencyAmount());

                    String region = adjustmentAmount.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());

                    final AdjustmentItemList adjustmentItemList = adjustmentEvent.getAdjustmentItemList();
                    if(CollectionUtil.isNotEmpty(adjustmentItemList)){
                        tmp.setMsku(adjustmentItemList.get(0).getSellerSKU());
                    }

                    list.add(tmp);
                }

//                final AdjustmentItemList adjustmentItemList = adjustmentEvent.getAdjustmentItemList();
//                if(null == adjustmentItemList){
//                    continue;
//                }
//                for(AdjustmentItem adjustmentItem : adjustmentItemList){
//                    final Currency totalAmount = adjustmentItem.getTotalAmount();
//
//                    if(null != totalAmount){
//                        TmpSettlementReport tmp = new TmpSettlementReport();
//                        tmp.setFinancialNo(financialNo);
//                        tmp.setAccountType("Standard");
//                        tmp.setEventType("Adjustment");
//                        tmp.setMsku(adjustmentItem.getSellerSKU());
//                        tmp.setAmountDescription(adjustmentItem.getProductDescription());
//                        tmp.setQuantityPurchased(Integer.valueOf(adjustmentItem.getQuantity()));
//                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
//                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
//                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
//                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
//                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
//                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
//                        tmp.setCurrency(totalAmount.getCurrencyCode());
//                        tmp.setAmount(totalAmount.getCurrencyAmount());
//
//                        String region = totalAmount.getCurrencyCode().substring(0,2);
//                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
//                        tmp.setSid(shopInfo.getId());
//                        list.add(tmp);
//                    }
//                }
            }
        }
        //Chargeback 退款事件
        final ShipmentEventList chargebackEventList = financialEvents.getChargebackEventList();
        if(CollectionUtil.isNotEmpty(chargebackEventList)){
            disposeShipmentEventList(shopList,chargebackEventList,financialEventGroup,"Chargeback",list);
        }
        //ProductAdsPayment
        final ProductAdsPaymentEventList productAdsPaymentEventList = financialEvents.getProductAdsPaymentEventList();
        if(CollectionUtil.isNotEmpty(productAdsPaymentEventList)){
            for(ProductAdsPaymentEvent productAdsPaymentEvent : productAdsPaymentEventList){
                final String postedDate = productAdsPaymentEvent.getPostedDate();
                final Currency baseValue = productAdsPaymentEvent.getBaseValue();
                if(null != baseValue && baseValue.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = baseValue.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("ProductAdsPayment");
                    tmp.setCurrency(baseValue.getCurrencyCode());
                    tmp.setAmountDescription("baseValue");
                    tmp.setAmount(baseValue.getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
                final Currency taxValue = productAdsPaymentEvent.getTaxValue();
                if(null != taxValue && taxValue.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = baseValue.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("ProductAdsPayment");
                    tmp.setCurrency(baseValue.getCurrencyCode());
                    tmp.setAmountDescription("taxValue");
                    tmp.setAmount(baseValue.getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }
        }
        //AdhocDisbursement
        final AdhocDisbursementEventList adhocDisbursementEventList = financialEvents.getAdhocDisbursementEventList();
        if(CollectionUtil.isNotEmpty(adhocDisbursementEventList)){
            for(AdhocDisbursementEvent adhocDisbursementEvent : adhocDisbursementEventList){
                final Currency transactionAmount = adhocDisbursementEvent.getTransactionAmount();
                String region = transactionAmount.getCurrencyCode().substring(0,2);
                ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                TmpSettlementReport tmp = new TmpSettlementReport();
                tmp.setSid(shopInfo.getId());
                tmp.setFinancialNo(financialNo);
                tmp.setAccountType("Standard");
                tmp.setEventType("AdhocDisbursement");
                tmp.setCurrency(transactionAmount.getCurrencyCode());
                tmp.setAmountDescription(adhocDisbursementEvent.getTransactionType());
                tmp.setAmount(transactionAmount.getCurrencyAmount());
                tmp.setPostedDateTime(DateUtil.parse(adhocDisbursementEvent.getPostedDate(), DatePattern.UTC_PATTERN));
                tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                list.add(tmp);
            }
        }

        //Shipment
        final ShipmentEventList shipmentEventList = financialEvents.getShipmentEventList();
        if(CollectionUtil.isNotEmpty(shipmentEventList)){
            disposeShipmentEventList(shopList,shipmentEventList,financialEventGroup,"Shipment",list);
        }

        //ShipmentSettle
        final ShipmentSettleEventList shipmentSettleEventList = financialEvents.getShipmentSettleEventList();
        if(CollectionUtil.isNotEmpty(shipmentSettleEventList)){
            disposeShipmentEventList(shopList,shipmentSettleEventList,financialEventGroup,"ShipmentSettle",list);
        }

        //DebtRecovery
        final DebtRecoveryEventList debtRecoveryEventList = financialEvents.getDebtRecoveryEventList();
        if(CollectionUtil.isNotEmpty(debtRecoveryEventList)){
            for(DebtRecoveryEvent debtRecoveryEvent : debtRecoveryEventList){
                final Currency recoveryAmount = debtRecoveryEvent.getRecoveryAmount();
                if(null != recoveryAmount && recoveryAmount.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = recoveryAmount.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("DebtRecovery");
                    tmp.setCurrency(recoveryAmount.getCurrencyCode());
                    tmp.setAmountDescription("RecoveryAmount");
                    tmp.setAmount(recoveryAmount.getCurrencyAmount());
//                tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }
        }
        //CapacityReservationBilling
        final CapacityReservationBillingEventList capacityReservationBillingEventList = financialEvents.getCapacityReservationBillingEventList();
        if(CollectionUtil.isNotEmpty(capacityReservationBillingEventList)){
            for(CapacityReservationBillingEvent capacityReservationBillingEvent : capacityReservationBillingEventList){
                final Currency transactionAmount = capacityReservationBillingEvent.getTransactionAmount();
                String region = transactionAmount.getCurrencyCode().substring(0,2);
                ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                TmpSettlementReport tmp = new TmpSettlementReport();
                tmp.setSid(shopInfo.getId());
                tmp.setFinancialNo(financialNo);
                tmp.setAccountType("Standard");
                tmp.setEventType("AdhocDisbursement");
                tmp.setCurrency(transactionAmount.getCurrencyCode());
                tmp.setAmountDescription(capacityReservationBillingEvent.getTransactionType());
                tmp.setAmount(transactionAmount.getCurrencyAmount());
                tmp.setPostedDateTime(DateUtil.parse(capacityReservationBillingEvent.getPostedDate(), DatePattern.UTC_PATTERN));
                tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                list.add(tmp);
            }
        }

        //TODO Liquidations Adjustments
        final FBALiquidationEventList fbALiquidationEventList = financialEvents.getFbALiquidationEventList();
//        for(FBALiquidationEvent fbaLiquidationEvent : fbALiquidationEventList){
//            fbaLiquidationEvent.getPostedDate();
//
//        }
        //RemovalShipment
        final RemovalShipmentEventList removalShipmentEventList = financialEvents.getRemovalShipmentEventList();
        if(CollectionUtil.isNotEmpty(removalShipmentEventList)){
            for(RemovalShipmentEvent removalShipmentEvent : removalShipmentEventList){
                final String postedDate = removalShipmentEvent.getPostedDate();
                final String orderId = removalShipmentEvent.getOrderId();
                final RemovalShipmentItemList removalShipmentItemList = removalShipmentEvent.getRemovalShipmentItemList();
                for(RemovalShipmentItem removalShipmentItem : removalShipmentItemList){
                    final String fnsku = removalShipmentItem.getFulfillmentNetworkSKU();
                    final Integer quantity = removalShipmentItem.getQuantity();

                    final Currency revenue = removalShipmentItem.getRevenue();
                    if(null != revenue){
                        String region = revenue.getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType("RemovalShipment");
                        tmp.setCurrency(revenue.getCurrencyCode());
                        tmp.setAmountDescription("Revenue");
                        tmp.setAmount(revenue.getCurrencyAmount());
                        tmp.setOrderId(orderId);
                        tmp.setMsku(fnsku);
                        tmp.setQuantityPurchased(quantity);
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                    final Currency feeAmount = removalShipmentItem.getFeeAmount();
                    if(null != feeAmount){
                        String region = feeAmount.getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType("RemovalShipment");
                        tmp.setCurrency(feeAmount.getCurrencyCode());
                        tmp.setAmountDescription("FeeAmount");
                        tmp.setAmount(feeAmount.getCurrencyAmount());
                        tmp.setOrderId(orderId);
                        tmp.setMsku(fnsku);
                        tmp.setQuantityPurchased(quantity);
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                    final Currency taxAmount = removalShipmentItem.getTaxAmount();
                    if(null != taxAmount){
                        String region = taxAmount.getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType("RemovalShipment");
                        tmp.setCurrency(taxAmount.getCurrencyCode());
                        tmp.setAmountDescription("TaxAmount");
                        tmp.setAmount(taxAmount.getCurrencyAmount());
                        tmp.setOrderId(orderId);
                        tmp.setMsku(fnsku);
                        tmp.setQuantityPurchased(quantity);
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                    final Currency taxWithheld = removalShipmentItem.getTaxWithheld();
                    if(null != taxWithheld){
                        String region = taxWithheld.getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType("RemovalShipment");
                        tmp.setCurrency(taxWithheld.getCurrencyCode());
                        tmp.setAmountDescription("TaxWithheld");
                        tmp.setAmount(taxWithheld.getCurrencyAmount());
                        tmp.setOrderId(orderId);
                        tmp.setMsku(fnsku);
                        tmp.setQuantityPurchased(quantity);
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                }
            }
        }

        //SellerDealPayment
        final SellerDealPaymentEventList sellerDealPaymentEventList = financialEvents.getSellerDealPaymentEventList();
        if(CollectionUtil.isNotEmpty(sellerDealPaymentEventList)){
            for(SellerDealPaymentEvent sellerDealPaymentEvent : sellerDealPaymentEventList){
                final String postedDate = sellerDealPaymentEvent.getPostedDate();
                final Currency feeAmount = sellerDealPaymentEvent.getFeeAmount();
                if(null != feeAmount && feeAmount.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = feeAmount.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("SellerDealPayment");
                    tmp.setCurrency(feeAmount.getCurrencyCode());
                    tmp.setAmountDescription("feeAmount");
                    tmp.setAmount(feeAmount.getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }

                final Currency taxAmount = sellerDealPaymentEvent.getTaxAmount();
                if(null != taxAmount && taxAmount.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = taxAmount.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("SellerDealPayment");
                    tmp.setCurrency(taxAmount.getCurrencyCode());
                    tmp.setAmountDescription("TaxAmount");
                    tmp.setAmount(taxAmount.getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }
        }
        //ServiceFee
        final ServiceFeeEventList serviceFeeEventList = financialEvents.getServiceFeeEventList();
        if(CollectionUtil.isNotEmpty(serviceFeeEventList)){
            for(ServiceFeeEvent serviceFeeEvent : serviceFeeEventList){
                final String amazonOrderId = serviceFeeEvent.getAmazonOrderId();
                final FeeComponentList feeList = serviceFeeEvent.getFeeList();
                if(null != feeList){
                    for(FeeComponent feeComponent : feeList){
                        final Currency feeAmount = feeComponent.getFeeAmount();
                        if(null !=  feeAmount && feeAmount.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                            TmpSettlementReport tmp = new TmpSettlementReport();
                            String region = feeAmount.getCurrencyCode().substring(0,2);
                            ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                            tmp.setSid(shopInfo.getId());
                            tmp.setFinancialNo(financialNo);
                            tmp.setOrderId(amazonOrderId);
                            tmp.setAccountType("Standard");
                            tmp.setEventType("ServiceFee");
                            tmp.setCurrency(feeAmount.getCurrencyCode());
                            tmp.setAmountDescription(feeComponent.getFeeType());
                            tmp.setAmount(feeAmount.getCurrencyAmount());
//                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                            tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                            tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                            tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                            tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                            tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                            list.add(tmp);
                        }
                    }
                }
            }
        }
        //Refund
        final ShipmentEventList refundEventList = financialEvents.getRefundEventList();
        if(CollectionUtil.isNotEmpty(refundEventList)){
            disposeShipmentEventList(shopList,refundEventList,financialEventGroup,"Refund",list);
        }
        //RemovalShipmentAdjustment
        final RemovalShipmentAdjustmentEventList removalShipmentAdjustmentEventList = financialEvents.getRemovalShipmentAdjustmentEventList();
        if(CollectionUtil.isNotEmpty(removalShipmentAdjustmentEventList)){
            for(RemovalShipmentAdjustmentEvent removalShipmentAdjustmentEvent : removalShipmentAdjustmentEventList){
                final String postedDate = removalShipmentAdjustmentEvent.getPostedDate();
                final String orderId = removalShipmentAdjustmentEvent.getOrderId();
                final List<RemovalShipmentItemAdjustment> removalShipmentItemAdjustmentList = removalShipmentAdjustmentEvent.getRemovalShipmentItemAdjustmentList();
                for(RemovalShipmentItemAdjustment removalShipmentItemAdjustment : removalShipmentItemAdjustmentList){
                    final String sku = removalShipmentItemAdjustment.getFulfillmentNetworkSKU();
                    final Integer quantity = removalShipmentItemAdjustment.getAdjustedQuantity();

                    final Currency revenueAdjustment = removalShipmentItemAdjustment.getRevenueAdjustment();
                    if(null != revenueAdjustment){
                        String region = revenueAdjustment.getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType("RemovalShipmentAdjustment");
                        tmp.setCurrency(revenueAdjustment.getCurrencyCode());
                        tmp.setAmountDescription("RevenueAdjustment");
                        tmp.setAmount(revenueAdjustment.getCurrencyAmount());
                        tmp.setOrderId(orderId);
                        tmp.setMsku(sku);
                        tmp.setQuantityPurchased(quantity);
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }

                    final Currency taxAmountAdjustment = removalShipmentItemAdjustment.getTaxAmountAdjustment();
                    if(null != taxAmountAdjustment){
                        String region = taxAmountAdjustment.getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType("RemovalShipmentAdjustment");
                        tmp.setCurrency(taxAmountAdjustment.getCurrencyCode());
                        tmp.setAmountDescription("TaxAmountAdjustment");
                        tmp.setAmount(taxAmountAdjustment.getCurrencyAmount());
                        tmp.setOrderId(orderId);
                        tmp.setMsku(sku);
                        tmp.setQuantityPurchased(quantity);
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }

                    final Currency taxWithheldAdjustment = removalShipmentItemAdjustment.getTaxWithheldAdjustment();
                    if(null != taxWithheldAdjustment){
                        String region = taxWithheldAdjustment.getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType("RemovalShipmentAdjustment");
                        tmp.setCurrency(taxWithheldAdjustment.getCurrencyCode());
                        tmp.setAmountDescription("TaxWithheldAdjustment");
                        tmp.setAmount(taxWithheldAdjustment.getCurrencyAmount());
                        tmp.setOrderId(orderId);
                        tmp.setMsku(sku);
                        tmp.setQuantityPurchased(quantity);
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                }
            }
        }

        //CouponPayment
        final CouponPaymentEventList couponPaymentEventList = financialEvents.getCouponPaymentEventList();
        if(CollectionUtil.isNotEmpty(couponPaymentEventList)){
            for(CouponPaymentEvent couponPaymentEvent : couponPaymentEventList){
                final String postedDate = couponPaymentEvent.getPostedDate();
                final FeeComponent feeComponent = couponPaymentEvent.getFeeComponent();
                final Currency feeAmount = feeComponent.getFeeAmount();
                if(null !=  feeAmount && feeAmount.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = feeAmount.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("CouponPayment");
                    tmp.setCurrency(feeAmount.getCurrencyCode());
                    tmp.setAmountDescription(feeComponent.getFeeType());
                    tmp.setAmount(feeAmount.getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }

            }
        }
        //TrialShipment
        final TrialShipmentEventList trialShipmentEventList = financialEvents.getTrialShipmentEventList();
        if(CollectionUtil.isNotEmpty(trialShipmentEventList)){
            for(TrialShipmentEvent trialShipmentEvent : trialShipmentEventList){
                final String orderId = trialShipmentEvent.getAmazonOrderId();
                final String postedDate = trialShipmentEvent.getPostedDate();
                final String sku = trialShipmentEvent.getSKU();
                final FeeComponentList feeList = trialShipmentEvent.getFeeList();
                for(FeeComponent feeComponent : feeList){
                    final String feeType = feeComponent.getFeeType();
                    final Currency feeAmount = feeComponent.getFeeAmount();
                    String region = feeAmount.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("TrialShipment");
                    tmp.setCurrency(feeAmount.getCurrencyCode());
                    tmp.setAmountDescription(feeComponent.getFeeType());
                    tmp.setAmount(feeAmount.getCurrencyAmount());
                    tmp.setOrderId(orderId);
                    tmp.setMsku(sku);
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }
        }

        //Retrocharge
        final RetrochargeEventList retrochargeEventList = financialEvents.getRetrochargeEventList();
        if(CollectionUtil.isNotEmpty(retrochargeEventList)){
            for(RetrochargeEvent retrochargeEvent : retrochargeEventList){
                final String postedDate = retrochargeEvent.getPostedDate();
                final Currency baseTax = retrochargeEvent.getBaseTax();
                if(null != baseTax && baseTax.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = baseTax.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("Retrocharge");
                    tmp.setCurrency(baseTax.getCurrencyCode());
                    tmp.setAmountDescription("BaseTax");
                    tmp.setAmount(baseTax.getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
                final Currency shippingTax = retrochargeEvent.getShippingTax();
                if(null != shippingTax && shippingTax.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = shippingTax.getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType("Retrocharge");
                    tmp.setCurrency(shippingTax.getCurrencyCode());
                    tmp.setAmountDescription("ShippingTax");
                    tmp.setAmount(shippingTax.getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }

                final TaxWithheldComponentList retrochargeTaxWithheldList = retrochargeEvent.getRetrochargeTaxWithheldList();
                if(null != retrochargeTaxWithheldList){
                    for(TaxWithheldComponent taxWithheldComponent : retrochargeTaxWithheldList){
                        final ChargeComponentList taxesWithheld = taxWithheldComponent.getTaxesWithheld();
                        for(ChargeComponent chargeComponent : taxesWithheld ){
                            final Currency chargeAmount = chargeComponent.getChargeAmount();
                            if(null != chargeAmount && chargeAmount.getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                                TmpSettlementReport tmp = new TmpSettlementReport();
                                String region = chargeAmount.getCurrencyCode().substring(0,2);
                                ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                                tmp.setSid(shopInfo.getId());
                                tmp.setFinancialNo(financialNo);
                                tmp.setAccountType("Standard");
                                tmp.setEventType("Retrocharge");
                                tmp.setCurrency(chargeAmount.getCurrencyCode());
                                tmp.setAmountDescription(chargeComponent.getChargeType());
                                tmp.setAmount(chargeAmount.getCurrencyAmount());
                                tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                                tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                                tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                                tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                                tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                                tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                                list.add(tmp);
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    /**
     * 处理运输事件 ShipmentEventList
     * @param shopList
     * @param shipmentEventList
     * @param eventType
     * @param list
     */
    public void disposeShipmentEventList(List<ShopInfo> shopList,ArrayList<ShipmentEvent> shipmentEventList,FinancialEventGroup financialEventGroup,String eventType,List<TmpSettlementReport> list){
        for(ShipmentEvent shipmentEvent : shipmentEventList){

            final ShipmentItemList shipmentItemList = shipmentEvent.getShipmentItemList();
            if(null != shipmentItemList ){
                buildShipmentItemList(shopList,shipmentEvent,shipmentItemList,financialEventGroup,eventType,list);
            }

            final ShipmentItemList shipmentItemAdjustmentList = shipmentEvent.getShipmentItemAdjustmentList();
            if(null != shipmentItemAdjustmentList){
                buildShipmentItemList(shopList,shipmentEvent,shipmentItemAdjustmentList,financialEventGroup,eventType,list);
            }

        }
    }

    public void buildShipmentItemList(List<ShopInfo> shopList,ShipmentEvent shipmentEvent,ShipmentItemList shipmentItemList,FinancialEventGroup financialEventGroup,String eventType,List<TmpSettlementReport> list){
        final String amazonOrderId = shipmentEvent.getAmazonOrderId();
        final String sellerOrderId = shipmentEvent.getSellerOrderId();
        final String marketplaceName = shipmentEvent.getMarketplaceName();
        final String postedDate = shipmentEvent.getPostedDate();

        for(ShipmentItem shipmentItem : shipmentItemList ){
            final String msku = shipmentItem.getSellerSKU();
            final String orderItemId = shipmentItem.getOrderItemId();
            final String orderAdjustmentItemId = shipmentItem.getOrderAdjustmentItemId();
            final Integer quantityShipped = shipmentItem.getQuantityShipped();
            final ChargeComponentList itemChargeList = shipmentItem.getItemChargeList();
            if(null != itemChargeList){
                for(ChargeComponent chargeComponent : itemChargeList){
                    if(chargeComponent.getChargeAmount().getCurrencyAmount().compareTo(BigDecimal.ZERO) == 0){
                        continue;
                    }
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = chargeComponent.getChargeAmount().getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType(eventType);
                    tmp.setCurrency(chargeComponent.getChargeAmount().getCurrencyCode());
                    tmp.setOrderId(amazonOrderId);
                    tmp.setMerchantOrderId(sellerOrderId);
                    tmp.setMarketplaceName(marketplaceName);
                    tmp.setAmountDescription(chargeComponent.getChargeType());
                    tmp.setAmount(chargeComponent.getChargeAmount().getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setOrderItemCode(orderItemId);
                    tmp.setMsku(msku);
                    tmp.setQuantityPurchased(quantityShipped);
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }

            final ChargeComponentList itemChargeAdjustmentList = shipmentItem.getItemChargeAdjustmentList();
            if(null != itemChargeAdjustmentList){
                for(ChargeComponent chargeComponent : itemChargeAdjustmentList){
                    if(chargeComponent.getChargeAmount().getCurrencyAmount().compareTo(BigDecimal.ZERO) == 0){
                        continue;
                    }
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = chargeComponent.getChargeAmount().getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType(eventType);
                    tmp.setCurrency(chargeComponent.getChargeAmount().getCurrencyCode());
                    tmp.setOrderId(amazonOrderId);
                    tmp.setMerchantOrderId(sellerOrderId);
                    tmp.setMarketplaceName(marketplaceName);
                    tmp.setAmountDescription(chargeComponent.getChargeType());
                    tmp.setAmount(chargeComponent.getChargeAmount().getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setOrderItemCode(orderItemId);
                    tmp.setMsku(msku);
                    tmp.setQuantityPurchased(quantityShipped);
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }

            final FeeComponentList itemFeeList = shipmentItem.getItemFeeList();
            if(null != itemFeeList){
                for(FeeComponent feeComponent : itemFeeList){
                    if(feeComponent.getFeeAmount().getCurrencyAmount().compareTo(BigDecimal.ZERO) == 0){
                        continue;
                    }
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = feeComponent.getFeeAmount().getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType(eventType);
                    tmp.setCurrency(feeComponent.getFeeAmount().getCurrencyCode());
                    tmp.setOrderId(amazonOrderId);
                    tmp.setMerchantOrderId(sellerOrderId);
                    tmp.setMarketplaceName(marketplaceName);
                    tmp.setAmountDescription(feeComponent.getFeeType());
                    tmp.setAmount(feeComponent.getFeeAmount().getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setOrderItemCode(orderItemId);
                    tmp.setMsku(msku);
                    tmp.setQuantityPurchased(quantityShipped);
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }
            final FeeComponentList itemFeeAdjustmentList = shipmentItem.getItemFeeAdjustmentList();
            if(null != itemFeeAdjustmentList){
                for(FeeComponent feeComponent : itemFeeAdjustmentList){
                    if(feeComponent.getFeeAmount().getCurrencyAmount().compareTo(BigDecimal.ZERO) == 0){
                        continue;
                    }
                    TmpSettlementReport tmp = new TmpSettlementReport();
                    String region = feeComponent.getFeeAmount().getCurrencyCode().substring(0,2);
                    ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                    tmp.setSid(shopInfo.getId());
                    tmp.setFinancialNo(financialNo);
                    tmp.setAccountType("Standard");
                    tmp.setEventType(eventType);
                    tmp.setCurrency(feeComponent.getFeeAmount().getCurrencyCode());
                    tmp.setOrderId(amazonOrderId);
                    tmp.setMerchantOrderId(sellerOrderId);
                    tmp.setMarketplaceName(marketplaceName);
                    tmp.setAmountDescription(feeComponent.getFeeType());
                    tmp.setAmount(feeComponent.getFeeAmount().getCurrencyAmount());
                    tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                    tmp.setOrderItemCode(orderItemId);
                    tmp.setMsku(msku);
                    tmp.setQuantityPurchased(quantityShipped);
                    tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                    tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                    tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                    tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                    tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                    list.add(tmp);
                }
            }

            final TaxWithheldComponentList itemTaxWithheldList = shipmentItem.getItemTaxWithheldList();
            if(null != itemTaxWithheldList){
                for(TaxWithheldComponent taxWithheldComponent : itemTaxWithheldList){
                    final ChargeComponentList taxesWithheld = taxWithheldComponent.getTaxesWithheld();
                    for(ChargeComponent chargeComponent : taxesWithheld){
                        if(chargeComponent.getChargeAmount().getCurrencyAmount().compareTo(BigDecimal.ZERO) == 0){
                            continue;
                        }
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        String region = chargeComponent.getChargeAmount().getCurrencyCode().substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType(eventType);
                        tmp.setCurrency(chargeComponent.getChargeAmount().getCurrencyCode());
                        tmp.setOrderId(amazonOrderId);
                        tmp.setMerchantOrderId(sellerOrderId);
                        tmp.setMarketplaceName(marketplaceName);
                        tmp.setAmountDescription(chargeComponent.getChargeType());
                        tmp.setAmount(chargeComponent.getChargeAmount().getCurrencyAmount());
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setOrderItemCode(orderItemId);
                        tmp.setMsku(msku);
                        tmp.setQuantityPurchased(quantityShipped);
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                }
            }

            final PromotionList promotionList = shipmentItem.getPromotionList();
            if(null != promotionList){
                for(Promotion promotion : promotionList){
                    if(promotion.getPromotionAmount().getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                        String currency = promotion.getPromotionAmount().getCurrencyCode();
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        String region = currency.substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType(eventType);
                        tmp.setCurrency(currency);
                        tmp.setOrderId(amazonOrderId);
                        tmp.setMerchantOrderId(sellerOrderId);
                        tmp.setMarketplaceName(marketplaceName);
                        tmp.setAmountDescription(promotion.getPromotionId().equals("Amazon PLCC Free-Financing Universal Merchant Free Rush Shipping") ? "ShippingDiscount" : "PromotionDiscount");
                        tmp.setAmount(promotion.getPromotionAmount().getCurrencyAmount());
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setOrderItemCode(orderItemId);
                        tmp.setMsku(msku);
                        tmp.setQuantityPurchased(quantityShipped);
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                }

            }

            final PromotionList promotionAdjustmentList = shipmentItem.getPromotionAdjustmentList();
            if(null != promotionAdjustmentList){
                for(Promotion promotion : promotionAdjustmentList){
                    if(promotion.getPromotionAmount().getCurrencyAmount().compareTo(BigDecimal.ZERO) != 0){
                        String currency = promotion.getPromotionAmount().getCurrencyCode();
                        TmpSettlementReport tmp = new TmpSettlementReport();
                        String region = currency.substring(0,2);
                        ShopInfo shopInfo = shopList.stream().filter(s -> s.getRegion().equals(region)).findAny().orElse(new ShopInfo());
                        tmp.setSid(shopInfo.getId());
                        tmp.setFinancialNo(financialNo);
                        tmp.setAccountType("Standard");
                        tmp.setEventType(eventType);
                        tmp.setCurrency(currency);
                        tmp.setOrderId(amazonOrderId);
                        tmp.setMerchantOrderId(sellerOrderId);
                        tmp.setMarketplaceName(marketplaceName);
                        tmp.setAmountDescription(promotion.getPromotionId().equals("Amazon PLCC Free-Financing Universal Merchant Free Rush Shipping") ? "ShippingDiscount" : "PromotionDiscount");
                        tmp.setAmount(promotion.getPromotionAmount().getCurrencyAmount());
                        tmp.setPostedDateTime(DateUtil.parse(postedDate, DatePattern.UTC_PATTERN));
                        tmp.setOrderItemCode(orderItemId);
                        tmp.setMsku(msku);
                        tmp.setQuantityPurchased(quantityShipped);
                        tmp.setFinancialEventGroupId(financialEventGroup.getFinancialEventGroupId());
                        tmp.setProcessingStatus(financialEventGroup.getProcessingStatus());
                        tmp.setFundTransferStatus(StrUtil.isBlank(financialEventGroup.getFundTransferStatus()) ? "Unknown" : financialEventGroup.getFundTransferStatus());
                        tmp.setSettlementStartDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupStart()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupStart(), DatePattern.UTC_PATTERN));
                        tmp.setSettlementEndDate(StrUtil.isBlank(financialEventGroup.getFinancialEventGroupEnd()) ? null : DateUtil.parse(financialEventGroup.getFinancialEventGroupEnd(), DatePattern.UTC_PATTERN));
                        list.add(tmp);
                    }
                }

            }
        }
    }
}
